﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using CloundUploader.Framework;
using MyUploader.UI;

namespace CloundUploader
{
    public partial class MainForm : Form, IComonent
    {
        public MainForm()
        {
            InitializeComponent();
            notifyIcon.Icon = this.Icon;
            notifyIcon.Text = this.Text;
            notifyIcon.Visible = true;
            //初始化菜单
            XmlDocument document = new XmlDocument();
            document.LoadXml(@"<req>
                                     <menu name='开始' enabled='true'>
                                          <item name='appclose'  enabled='true'>退出</item>
                                     </menu>
                                     <menu name='用户' enabled='true'>
                                          <item name='userlogin' enabled='true'>登陆</item>
                                          <item name='userchange'  enabled='true'>更改用户</item>
                                     </menu>
                                     <menu name='帮助' enabled='true'>
                                          <item name='userlogin' enabled='true'>关于</item>
                                     </menu>
                             </req>");
            foreach (XmlNode menu in document.FirstChild.SelectNodes("menu"))
            {
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Text = menu.Attributes["name"].Value;
                item.Enabled = bool.Parse(menu.Attributes["enabled"].Value);
                foreach (XmlNode it in menu.SelectNodes("item"))
                {
                    item.DropDownItems.Add(new ToolStripMenuItem { Text = it.InnerText.Trim(),Enabled = bool.Parse(menu.Attributes["enabled"].Value), Name = it.Attributes["name"].Value });
                }
                menuStrip.Items.Add(item);
            }

        }

        public string Key
        {
            get { return "mainform-8a54-456c-ab6a-1d4fd6de4ca1"; }
        }

       private void toolStripScheduler_Click(object sender, EventArgs e)
        {
            uploadList.StartScheduler(toolStripScheduler.Checked);
        }

        private void selectFilesStripButton_Click(object sender, EventArgs e)
        {
            var uploadForm=new NewSpicialUpload();
            uploadForm.ShowDialog();
        }

        private void toolStart_Click(object sender, EventArgs e)
        {
            uploadList.StartSelections();
        }

        private void toolPause_Click(object sender, EventArgs e)
        {
            uploadList.Pause();
        }

        private void toolPauseAll_Click(object sender, EventArgs e)
        {
            uploadList.PauseAll();
        }

        private void toolRemove_Click(object sender, EventArgs e)
        {
            uploadList.RemoveSelections();
        }

        private void toolRemoveCompleted_Click(object sender, EventArgs e)
        {
            uploadList.RemoveCompleted();
        }

        private void toolMoveSelectionsUp_Click(object sender, EventArgs e)
        {
            uploadList.MoveSelectionsUp();
        }

        private void toolMoveSelectionsDown_Click(object sender, EventArgs e)
        {
            uploadList.MoveSelectionsDown();
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ShowHideForm();
        }
        public void ShowHideForm()
        {
            if (this.Visible)
            {
                HideForm();
            }
            else
            {
                ShowForm();
            }
        }
        public void HideForm()
        {
            this.ShowInTaskbar = false;
            this.Hide();
            notifyIcon.Visible = true;
        }
        public void ShowForm()
        {
            this.ShowInTaskbar = true;
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }
    }
}
